package org.zjx.analysis;

import lombok.Getter;
import org.zjx.core.IWindow;
import org.zjx.entity.Address;
import org.zjx.entity.ParagraphBean;
import org.zjx.enums.FontType;
import org.zjx.identify.PaneUtil;
import org.zjx.util.ImageUtil;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
public class GameAnalysis extends BaseAnalysis {
    private int hpPercentageRole; //血量百分比
    private int injuryPercentageRole; //伤势百分比
    private int mpPercentageRole; //蓝量百分比
    private int exePercentageRole; //经验百分比

    private int hpPercentagePet; //血量百分比
    private int mpPercentagePet; //蓝量百分比
    private int exePercentagePet; //经验百分比

    private boolean payment; //欠费

    private boolean haveWindow; //有打开的窗口

    private Address address;
    private boolean intBattle; //战斗中
    private boolean inTeam;    //有队中

    public GameAnalysis(IWindow window, BufferedImage screenshot) {
        super(window, screenshot);
    }

    @Override
    protected void analysis() {
        this.address = analysisAddress();
    }

    @Override
    protected void initResources() {

    }

    private Address analysisAddress() {
        BufferedImage addressImage = ImageUtil.cutImage(screenshot, addressArea());
        PaneUtil paneUtil = new PaneUtil(addressImage, 3, Collections.singletonList(new Color(104, 100, 104)));
        List<Rectangle> rects = paneUtil.detectRect();
        if (rects.isEmpty()) return null;
        else {
            Rectangle rectangle = rects.get(0);
            List<ParagraphBean.Word> words = identifyFont(conversion(addressArea(), rectangle), Collections.singletonList(Color.WHITE), FontType.LOCATION,0,false);
            String addressStr = wordToStr(words);
            return matcherLocation(addressStr);
        }
    }


    public Rectangle addressArea() {
        return ImageUtil.createRect(11, 23, 137, 54);
    }

    @Override
    public Rectangle closeArea() {
        return null;
    }

    @Override
    public Rectangle getOriginBounds() {
        return null;
    }

    @Override
    public Rectangle getRefImageArea() {
        return null;
    }

    @Override
    public BufferedImage getRefImage() {
        return null;
    }

    private Address matcherLocation(String location) {
        String regex = "([\\w\\u4e00-\\u9fa5]+)【([0-9]+)，([0-9]+)】";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(location);
        if (matcher.find()) {
            String city = matcher.group(1);
            int x = Integer.parseInt(matcher.group(2));
            int y = Integer.parseInt(matcher.group(3));
            return new Address(city, x, y);
        } else {
            return null;
        }
    }
}
